package com.java2104.controller;

        import com.java2104.entity.Job;
        import com.java2104.service.JobService;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.Controller;
        import org.springframework.ui.Model;
        import org.springframework.web.bind.annotation.*;

        import java.util.ArrayList;
        import java.util.Iterator;
        import java.util.List;
        import java.util.Map;

@Controller
@RequestMapping("/job")
public class JobController {
    @Autowired
    private JobService jobService;
    @GetMapping("/list")
    @ResponseBody
    public List<Job> selectJobName(String name){
        return jobService.selectJobName(name);
    }
    @RequestMapping("/showUpdateJob")
    public void  findByPrimaryKey(Model model, Integer id){
        model.addAttribute(jobService.get(id));
    }
    @PostMapping("/update")
    @ResponseBody
    public Map update(Job job){
        return jobService.update(job);
    }
    @PostMapping("/add")
    @ResponseBody
    public Map add(Job job ){
        return jobService.insertJob(job);
    }
    /*@PostMapping("/delete")
    @ResponseBody
    public String delete(@RequestBody List<Integer> ids){
        int a=jobService.delete(ids);
        return "成功删除"+a+"记录";
    }*/

    @PostMapping("/delete")
    @ResponseBody
    //ids是删除时候选中的，getIds是工作表里所有的工作
    public String delete_b(@RequestBody List<Integer> ids){
        Integer sum = 0;
        for (int i = 0; i < ids.size(); i++) {
            Integer count = jobService.getCountOfJobId(ids.get(i));
            //select count(*) from employee_inf where job_id=#{param1}
            if(count==0){
                int c = jobService.delete(ids.get(i));
                sum++;
            }
        }
        return sum+"job(s)success,"+(ids.size()-sum)+"job(s) failed";
    }
}
